﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_SearchResult : System.Web.UI.Page
{
    public void checkAlreadyLogin()
    {
        if ( Session["UserID"].ToString() == null||Session["QuanTri"].ToString()==null)
            Response.Redirect("Login.aspx");
    }
    SanPhamCtr SanPhamCtr = new SanPhamCtr();
    TimKiemCtr timKiemCtr = new TimKiemCtr();
    PhanLoaiCtr phanLoaiCtr = new PhanLoaiCtr();
    ThanhVienCtr thanhVienCtr = new ThanhVienCtr();
    TinTucCtr tinTucCtr = new TinTucCtr();
    void laySanPham()
    {

        gvSanPham.DataSource = SanPhamCtr.GetSanPhamH();
        gvSanPham.DataBind();
        gvSanPham.DataKeyNames = new string[] { "MaSP" };
        
    }
    void layPhanLoai()
    {
        
        gvPhanLoai.DataSource = phanLoaiCtr.layPhanLoaiH();
        gvPhanLoai.DataBind();
       
        


    }
    private void layTinTuc()
    {
        gvTinTuc.DataSource = tinTucCtr.layTinTucH();
        gvTinTuc.DataBind();
        
    }
    void Lien_ket_DL(string pCot)//Sap xep
    {

        if (pCot == "")
        {

            gvSanPham.DataSource = SanPhamCtr.GetSanPhamH();
        }
        else
        {
            gvSanPham.DataSource = SanPhamCtr.GetSanPham1H(pCot);
        }




        gvSanPham.DataBind();
        gvSanPham.DataKeyNames = new string[] { "MaSP" };
        

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            checkAlreadyLogin();
            string LoaiTK = Request.QueryString["LoaiTK"];
            string chuoi = Request.QueryString["ChuoiTimKiem"];
            if (LoaiTK == "" && chuoi == "")
            {
                MultiView1.Visible =false;
               // gvSanPham.Visible = false;
                lbLoi.Text = "Không có kết quả";
                return;
                
            }
            switch (int.Parse(LoaiTK))
            {
                case 1:
                    lbLoi.Text = "";
                    MultiView1.ActiveViewIndex = 0;
                    if (Request.QueryString["ChuoiTimKiem"] == "")
                    {
                        laySanPham();
                    }
                    else
                    {
                        string tenSP = Request.QueryString["ChuoiTimKiem"];
                        DataTable BangSP=timKiemCtr.TimKiemSPTheoTen(tenSP);
                        if (BangSP.Rows.Count > 0)
                        {
                            gvSanPham.DataSource = BangSP;
                            gvSanPham.DataBind();
                            gvSanPham.DataKeyNames = new string[] { "MaSP" };
                        }
                        else
                        {
                            lbLoiSP.Text = "Không có sản phẩm nào!";
                        }
                        
                    }
                    break;
                case 2:
                    lbLoi.Text = "";
                    MultiView1.ActiveViewIndex = 1;
                    if (Request.QueryString["ChuoiTimKiem"] == "")
                    {
                        layPhanLoai();
                    }
                    else
                    {
                        string tenLoaiSP = Request.QueryString["ChuoiTimKiem"];
                        DataTable BangPL=timKiemCtr.TimKiemLoaiSPTheoTen(tenLoaiSP);
                        if (BangPL.Rows.Count > 0)
                        {
                            gvPhanLoai.DataSource = BangPL;
                            gvPhanLoai.DataBind();
                        }
                        else
                        {
                            lbLoiPL.Text = "Không có loại Sản phẩm nào";
                        }
                        
                    }
                    
                    break;
                case 3:
                    lbLoi.Text = "";
                    MultiView1.ActiveViewIndex = 2;
                    if (Request.QueryString["ChuoiTimKiem"] == "")
                    {
                        gvKhachHang.DataSource = thanhVienCtr.GetThanhVien1H();
                        gvKhachHang.DataBind();
                    }
                    else
                    {
                        string tenThanhVien = Request.QueryString["ChuoiTimKiem"];
                        DataTable BangKH = timKiemCtr.TimKiemThanhVienTheoTen(tenThanhVien); ;
                       if (BangKH.Rows.Count > 0)
                       {
                           gvKhachHang.DataSource = BangKH;
                           gvKhachHang.DataBind();
                           //MultiView1.ActiveViewIndex = 0;
                       }
                       else
                       {
                           lbLoiKH.Text = "Không có loại Khách hàng nào";
                       }
                    }
                    break;
                case 4:
                    lbLoi.Text = "";
                    MultiView1.ActiveViewIndex = 3;
                    if (Request.QueryString["ChuoiTimKiem"] == "")
                    {
                        layTinTuc();
                    }
                    else
                    {
                        string tieuDe = Request.QueryString["ChuoiTimKiem"];
                        DataTable BangTT = timKiemCtr.TimKiemTinTucTheoTen(tieuDe);
                        if (BangTT.Rows.Count > 0)
                        {
                            gvTinTuc.DataSource = BangTT;
                            gvTinTuc.DataBind();
                        }
                        else
                        {
                            lbLoiTT.Text = "Không có loại Tin Tức nào";
                        }
                        
                    }
                    break;
            }
            
        }

    }
    protected void gvSanPham_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btn = (LinkButton)e.Row.Cells[4].Controls[1];
            //khi click vao nut xoa se phat sinh doan ma javascript thong bao xac nhan co muon xoa hay khong
            btn.OnClientClick = "return confirm('Bạn có chắc chắn xóa sản phẩm : " + Server.HtmlDecode(e.Row.Cells[1].Text) + " này không?')";
        }
    }
    protected void gvSanPham_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvSanPham.PageIndex = e.NewPageIndex;
        if (Request.QueryString["ChuoiTimKiem"] == "")
        {
            laySanPham();
        }
        else
        {
            string tenSP = Request.QueryString["ChuoiTimKiem"];
            gvSanPham.DataSource = timKiemCtr.TimKiemSPTheoTen(tenSP);
            gvSanPham.DataBind();
            gvSanPham.DataKeyNames = new string[] { "MaSP" };

        }
    }
    protected void gvSanPham_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string MaSP = gvSanPham.DataKeys[e.RowIndex].Value.ToString();

        SanPhamCtr SanPhamCtr = new SanPhamCtr();
        bool kq = SanPhamCtr.DeleteSanPhamH(MaSP);
        if (kq)
        {

            Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Xóa sản phẩm thành công');window.location=\"ProductManager.aspx?ChuoiTimKiem=\"</script>");
        }
        else
        {
            Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Xóa sản phẩm thất bại!');window.location=\"ProductManager.aspx?ChuoiTimKiem=\"</script>");
        }
        laySanPham();
    }
    protected void gvSanPham_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {


            if (e.CommandName.Equals("Edit"))
            {
                int maSP = int.Parse(e.CommandArgument.ToString());

                Response.Redirect("EditProduct.aspx?id=" + maSP);
            }
        }
        catch
        {
        }

    }
    protected void gvSanPham_Sorting(object sender, GridViewSortEventArgs e)
    {
        Lien_ket_DL(e.SortExpression);
    }

    protected void gvPhanLoai_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvPhanLoai.PageIndex = e.NewPageIndex;
        if (Request.QueryString["ChuoiTimKiem"] == "")
        {
            layPhanLoai();
        }
        else
        {
            string tenLoaiSP = Request.QueryString["ChuoiTimKiem"];
            gvPhanLoai.DataSource = timKiemCtr.TimKiemLoaiSPTheoTen(tenLoaiSP);
            gvPhanLoai.DataBind();

        }
    }
    protected void gvKhachHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvKhachHang.PageIndex = e.NewPageIndex;
        if (Request.QueryString["ChuoiTimKiem"] == "")
        {
            gvKhachHang.DataSource = thanhVienCtr.GetThanhVien1H();
            gvKhachHang.DataBind();
        }
        else
        {
            string tenThanhVien = Request.QueryString["ChuoiTimKiem"];
            gvKhachHang.DataSource = timKiemCtr.TimKiemThanhVienTheoTen(tenThanhVien);
            gvKhachHang.DataBind();
            //MultiView1.ActiveViewIndex = 0;
        }
    }
    protected void gvTinTuc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTinTuc.PageIndex = e.NewPageIndex;
        if (Request.QueryString["ChuoiTimKiem"] == "")
        {
            layTinTuc();
        }
        else
        {
            string tieuDe = Request.QueryString["ChuoiTimKiem"];
            gvTinTuc.DataSource = timKiemCtr.TimKiemTinTucTheoTen(tieuDe);
            gvTinTuc.DataBind();

        }

    }
}